<?php
namespace app\circle\controller;
use app\circle\logic\CircleMsgLogic;

/**
 * 工作圈消息api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/26
 * Time: 09:56
 */
class CircleMsg extends Base {

    public function __construct(){
        parent::__construct();
        $this->cm_logic = new CircleMsgLogic();
    }

    /**
     * 获取用户信息
     * @return \think\response\Json
     */
    public function ajax_get_user(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->cm_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = [];
        $maps[] = ['openid','=',$u_detail['openid']];
        $v_detail = model('visitor')->get_detail($maps,'id,headimgurl');
        $u_detail['headimgurl'] = $v_detail['headimgurl'];

        $user_info = [];
        $user_info['username'] = $u_detail['username'];
        $user_info['role'] = $u_detail['role'];
        $user_info['headimgurl'] = $v_detail['headimgurl'];
        $user_info['user_power'] = $u_detail['user_power'];

        $data['user_info'] = $user_info;
        return json($this->cm_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取工作圈消息
     * @return \think\response\Json
     */
    public function ajax_get_wcm(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->cm_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = $this->cm_logic->ajax_get_wcm_maps();
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_circle_msg')->get_list($maps,'*','is_top desc,id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $media_arr = json_decode($v['media'],true);
            //if($media_arr['type'] == 'video'){
                //$media_data = $media_arr['data'];
                //array_map(function ($value,$key) use (&$media_data){
                    //$video_url = deal_video_url($value['video_url']);
                    //$media_data[$key]['video_cover_pic'] = $video_url['video_cover_pic'];
                //},$media_data,array_keys($media_data));
                //$media_arr['data'] = $media_data;
            //}
            $list[$k]['media'] = $media_arr;

            //判断是否是自己的消息
            $list[$k]['is_myself'] = ($u_detail['openid'] == $v['openid']) ? 1 : 0;

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $user_detail = model('user')->get_detail($u_maps,'id,openid,username');
            $list[$k]['username'] = $user_detail['username'];

            $v_maps = [];
            $v_maps[] = ['openid','=',$user_detail['openid']];
            $v_detail = model('visitor')->get_detail($v_maps,'id,headimgurl');
            $list[$k]['headimgurl'] = $v_detail['headimgurl'];

            //查找点赞用户
            $wcc_maps = [];
            $wcc_maps[] = ['wcmid','=',$v['id']];
            $wcc_maps[] = ['type','=',2];
            $wcc_openids = model('work_circle_comment')->get_column($wcc_maps,'openid');

            $u_maps = [];
            $u_maps[] = ['openid','in',$wcc_openids];
            $wcc_username_arr = model('user')->get_column($u_maps,'username');
            $list[$k]['wcc_username'] = $wcc_username_arr;

            //判断是否点赞
            $list[$k]['is_wcc'] = in_array($u_detail['openid'],$wcc_openids) ? 1 : 0;

            //查找评论
            $wcc_maps = [];
            $wcc_maps[] = ['wcmid','=',$v['id']];
            $wcc_maps[] = ['type','=',1];
            $wcc_list = model('work_circle_comment')->get_list($wcc_maps,'id,openid,content');
            foreach($wcc_list as $k_2 => $v_2){
                $u_maps = [];
                $u_maps[] = ['openid','=',$v_2['openid']];
                $user_detail = model('user')->get_detail($u_maps,'username');
                $wcc_list[$k_2]['username'] = $user_detail['username'];
            }
            $list[$k]['wcc_list'] = $wcc_list;
        }

        $data['list'] = $list;
        $data['page'] = $params['page'];
        return json($this->cm_logic->get_response(1,'OK',$data));
    }

    /**
     * 发布工作圈消息
     * @return \think\response\Json
     */
    public function ajax_send_wcm(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->cm_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查发布工作圈消息规则
        $r = $this->cm_logic->check_send_wcm_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wcm_data = [];
        $wcm_data['openid'] = $u_detail['openid'];
        $wcm_data['content'] = trim($params['content']);
        $wcm_data['media'] = json_encode($params['media'],JSON_UNESCAPED_UNICODE);
        $wcm_data['is_top'] = $params['is_top'];
        $wcm_data['top_out_time'] = $params['top_out_time'];
        $wcmid = model('work_circle_msg')->insertGetId($wcm_data);
        if($wcmid){
            //更新发布工作圈积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_r_circle_integral($wcmid);

            return json($this->cm_logic->get_response(1,'发布成功'));
        }
        return json($this->cm_logic->get_response(0,'发布失败'));
    }

    /**
     * 置顶工作圈消息
     * @return \think\response\Json
     */
    public function ajax_is_top_wcm(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->cm_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查置顶工作圈消息规则
        $r = $this->cm_logic->check_is_top_wcm_rule($u_detail['openid'],$params);
        if(!$r['code']){
            return json($r);
        }
        $wcm_detail = $r['data']['wcm_detail'];

        $wcm_data = [];
        $wcm_data['is_top'] = $params['is_top'];
        $wcm_data['top_out_time'] = $params['top_out_time'];

        $maps = [];
        $maps[] = ['id','=',$wcm_detail['id']];
        $r = model('work_circle_msg')->force()->save($wcm_data,$maps);
        if($r !== false){
            return json($this->cm_logic->get_response(1,'操作成功'));
        }
        return json($this->cm_logic->get_response(0,'操作失败'));
    }

    /**
     * 删除工作圈消息
     * @return \think\response\Json
     */
    public function ajax_del_wcm(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $r = model('work_circle_msg')->destroy($params['wcmid']);
        if($r){
            return json($this->cm_logic->get_response(1,'删除成功'));
        }
        return json($this->cm_logic->get_response(0,'删除失败'));
    }
}
